<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Released under the MIT License.
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\app\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\AdminMenuModel;
use think\Validate;
class BannerController extends AdminBaseController
{
    public function index()
    {
        $banner = db::name('admin_banner')->order('sort desc')->paginate(5);
        $this->assign('banner',$banner);
        //$users->appends($params);  //分页条件
        $this->assign('page', $banner->render());
        return $this->fetch();
    }

    public function add()
    {
        $cate = config()['app']['bannerModel'];
        if($_POST){
            $validate = new Validate([
                'url'  => 'require',
                'img' => 'require',
            ]);
            if(!$validate->check($_POST)){
                $this->error($validate->getError());
            }
            $data['img'] = '/upload/'.$_POST['img'];
            $data['url'] = $_POST['url'];
            if($_POST['sort']){
                $data['sort'] = $_POST['sort'];
            }
            $data['model'] = $_POST['model'];
            $data['remark'] = $cate[$data['model']];
            $data['addtime'] = date('Y-m-d H:i:s',time());
            $res = db::name('admin_banner')->insert($data);
            if($res){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }
        $this->assign('cate',$cate);
        return $this->fetch();
    }
    public function edit()
    {
        $id = $this->param('id');
        $cate = config()['app']['bannerModel'];
        if($_POST){
            $validate = new Validate([
                'url'  => 'require',
            ]);
            if(!$validate->check($_POST)){
                $this->error($validate->getError());
            }
            if($_POST['img']){
                $data['img'] = '/upload/'.$_POST['img'];
            }
            if($_POST['sort']){
                $data['sort'] = $_POST['sort'];
            }
            $data['url'] = $_POST['url'];
            $data['model'] = $_POST['model'];
            $data['remark'] = $cate[$data['model']];
            $data['id'] = $_POST['id'];
            $res = db::name('admin_banner')->update($data);
            if($res){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }

        }else{
            $find = db::name('admin_banner')->find($id);
            $this->assign('cate',$cate);
            $this->assign('info',$find);
            return $this->fetch();
        }
    }
    public function delete()
    {
        $id = $this->param('id');
        $res = db::name('admin_banner')->delete($id);
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
}
